package com.cmict.data.form.controller.admin;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.form.api.dal.dataobject.info.MdColumnsDO;
import com.cmict.data.form.api.entity.vo.MdColumnsPageReqVO;
import com.cmict.data.form.api.entity.vo.MdColumnsRespVO;
import com.cmict.data.form.api.service.MdColumnsService;
import com.cmict.data.form.api.convert.MdColumnsConvert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;


@Api(tags = "管理后台 - 字段信息")
@RestController
@RequestMapping("/form/columns")
@Validated
public class MdColumnsController {

    @Resource
    private MdColumnsService mdColumnsService;

    @GetMapping("/get")
    @ApiOperation("获得字段信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('t:md-columns:query')")
    public CommonResult<MdColumnsRespVO> getMdColumns(@RequestParam("id") String id) {
        MdColumnsDO mdColumns = mdColumnsService.getMdColumns(id);
        return success(MdColumnsConvert.INSTANCE.convert(mdColumns));
    }

    @GetMapping("{tableId}/get")
    @ApiOperation("根据tableId获得字段信息")
    @ApiImplicitParam(name = "tableId", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<List<MdColumnsRespVO>> getMdColumnsByTableId(@PathVariable("tableId") String tableId) {
        List<MdColumnsDO> mdColumns = mdColumnsService.getMdColumnsByTableId(tableId);
        return success(MdColumnsConvert.INSTANCE.convertList(mdColumns));
    }


    @GetMapping("/list")
    @ApiOperation("获得字段信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('t:md-columns:query')")
    public CommonResult<List<MdColumnsRespVO>> getMdColumnsList(@RequestParam("ids") Collection<String> ids) {
        List<MdColumnsDO> list = mdColumnsService.getMdColumnsList(ids);
        return success(MdColumnsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得字段信息分页")
    @PreAuthorize("@ss.hasPermission('t:md-columns:query')")
    public CommonResult<PageResult<MdColumnsRespVO>> getMdColumnsPage(@Valid MdColumnsPageReqVO pageVO) {
        PageResult<MdColumnsDO> pageResult = mdColumnsService.getMdColumnsPage(pageVO);
        return success(MdColumnsConvert.INSTANCE.convertPage(pageResult));
    }

}
